<?php

namespace App\Admin\Controllers;

use App\Models\JumpTag;
use App\Models\Settings;
use Dcat\Admin\Form;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Http\JsonResponse;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Tab;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class MiniProgramRotationChatController extends AdminController
{

    const GROUP = 'special';

    public function index(Content $content)
    {
        $slug = request()->input('mini_type');

        $tab = Tab::make();
        $tab->addLink('首页模块管理', admin_route('mini_program_special.index', ['mini_type' => $slug]));
        $tab->addLink('底部菜单管理', admin_route('mini_program_tab.index', ['mini_type' => $slug]));
        $tab->addLink('首页导航', admin_route('mini_main_tab.index', ['mini_type' => $slug]));
        $tab->addLink('小程序导航', admin_route('mini_main_nav.index', ['mini_type' => $slug]));
        $tab->add('轮播图设置', $this->grid(), true);
        $tab->addLink('单图设置', admin_route('mini_single_img.index', ['mini_type' => $slug]));
        return $content->body($tab->withCard());
    }

    protected function grid()
    {
        $form = new Form();

        $form->action(admin_route('mini_rotation_chat.store'));

        $slug = request()->input('mini_type');
        $settings = getMiniSettings($slug, self::GROUP);

        $form->hidden('mini_type')->value($slug);

        $data = $settings['rotation_chat'] ?? '';
        if ($data) {
            $data = json_decode($data, true) ?: [];
        } else {
            $data = [];
        }

        $form->array('rotation_chat', '配置', function ($table) {
            $table->image('img', '图片')->url('upload/files')->uniqueName()->autoUpload()->saveFullUrl()->required();
            $table->select('jump_id', '跳转标签')->options(JumpTag::where('is_show', 1)->pluck('name', 'id')->all());
        })->value($data);

        $form->disableHeader();
        $form->disableResetButton();
        $form->disableEditingCheck();
        $form->disableCreatingCheck();
        $form->disableViewCheck();

        return $form;
    }

    public function store()
    {
        $type = request()->input('mini_type');
        $banners = array_values(request()->input('rotation_chat', []));

        foreach ($banners as $k => &$banner) {
            if ($banner['_remove_'] ?? 0) {
                unset($banners[$k]);
                continue;
            }

            if (!isset($banner['img']) || !$banner['img']) return JsonResponse::make()->error('图片必传');

            if (!Str::startsWith($banner['img'], ['http://', 'https://', '://'])) {
                $banner['img'] = Storage::disk(config('admin.upload.disk'))->url($banner['img']);
            }
        }

        Settings::updateOrCreate([
            'group'     => self::GROUP,
            'name'      => 'rotation_chat',
            'mini_type' => $type,
        ], ['value' => json_encode($banners)]);

        flushMiniSettings($type, self::GROUP);

        return JsonResponse::make()->success('保存成功');
    }
}
